// #ifdef H5
// import ioH5 from 'socket.io-client'
// #endif
// #ifdef MP
// #endif
import io from '../../js_sdk/socket-io/weapp.socket.io.js'
// import io from 'socket.io-client'
import config from "../config/index";

import Vue from "vue";

export default class Socket {
    constructor(host, query = {}) {
        const vm = this;
        if (this.socket) this.socket.close();
        if (!host) host = config.socketHost;
        if (Vue.$global_util.Login.isRefreshToken === true) {
            uni.$once('refreshTokenSuccess', function () {
                vm.init(host, query)
            })
        } else {
            vm.init(host, query)
        }

    }

    init(host, query = {}) {
        const tokenData = Vue.$global_util.Login.getLogin();
        const socket = this.socket = io(`${host}`, {
            query: {
                token: tokenData.token,
                ...query
            },
            //     autoConnect: false,
            //     path: '/socket.io',
            reconnectionAttempts: 3,//放弃之前尝试重新连接的次数
            // transports: ['websocket','xhr-polling','jsonp-polling'],
            // // rejectUnauthorized: true,
        });
        // 连接成功
        socket.on('connect', function (data) {
            console.log('socket连接成功', data);
        });
        // 连接错误时触发
        socket.on('connect_error', function (error) {
            console.log('socket连接错误时触发', error.message);
        });
        // 在连接超时时触发
        socket.on('connect_timeout', function (data) {
            console.log('socket在连接超时时触发', data);
        });
        // 发生错误时触发
        socket.on('error', function (error) {
            console.log('socket发生错误时触发', error.message);
        });
        // 发生错误时触发
        socket.on('error', function (error) {
            console.log('socket发生错误时触发', error.message);
        });
        // 连接断开
        socket.on('disconnect', function (reason) {
            console.log('socket连接断开', reason);
            // if (reason === 'io server disconnect') {
            //     // 断开连接是由服务器启动的，您需要手动重新连接
            //     socket.connect();
            // }
            // // 否则套接字将自动尝试重新连接
        });
        // 重新连接成功后触发
        socket.on('reconnect', function (attemptNumber) {
            console.log('socket重新连接成功后触发', attemptNumber);
        });
        // 尝试重新连接时触发 - 重新连接尝试次数
        socket.on('reconnect_attempt', function (attemptNumber) {
            console.log('socket重新连接成功后触发 重新连接尝试次数', attemptNumber);
        });
        // 连接错误时触发
        socket.on('reconnect_error', function (error) {
            console.log('socket连接错误时触发（1）', error.message);
        });
        // 连接错误时触发
        socket.on('reconnect_failed', function (error) {
            console.log('socket连接错误时触发（2）');
        });
        // 尝试重新连接时触发
        socket.on('reconnecting', function (attemptNumber) {
            console.log('socket重新连接成功后触发', attemptNumber);
        });
    }

    emit(event, data, callback) {
        this.socket && this.socket.emit(...arguments);
    }

    on(event, callback) {
        this.socket && this.socket.on(...arguments);
    }

    close() {
        this.socket && this.socket.close();
    }

    open() {
        this.socket && this.socket.open();
    }
}
